import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {ActivatedRoute, Router} from '@angular/router';
import {appConfig} from '../../../service/common';
import {UtilityService} from '../../../service/utils.service';
import {PageModule} from '../../../service/common.module';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import * as moment from 'moment';
import {ReuseTabService} from '@delon/abc';

@Component({
  selector: 'app-authority',
  templateUrl: './authority.component.html',
})

export class AuthorityComponent implements OnInit {

    searchOptions; // 选择显示的内容
    selectedMultipleOption; // 多选的内容
    groupGuid: string;
    showAdd: boolean;
    configTitle: string;
    total: number;
    array = []; // 定义数组 用来清空
    loading = false;
    page: any;
    pages: PageModule = new PageModule();

    data: any[] = []; // 表格数据
    headerData = [  // 配置表头内容
        { value: '应用名称', key: 'appName', isclick: false },
        { value: '应用类型', key: 'appType', isclick: false },
        { value: '应用开通时间', key: 'openDate', isclick: false },

    ];
    moreData = {
        morebutton: true,
        buttons: [
            { key: 'Overview', value: '查看概况' }
        ]
    };

    selectedRows: any;

    constructor(
        private http: _HttpClient,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) { }

    ngOnInit() {
        this.groupGuid = this.activatedRoute.snapshot.params.id; // 拿到父组件传过来的组织机构的guid来进行操作
        this.showAdd = true;
        this.configTitle = '删除'
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.omGroups + '/' + this.groupGuid)
            .subscribe(
                (val) => {
                    this.reuseTabService.title = val.result.groupName + '下应用'
                },
            );
        this.querygroupApp();
    }



    // 查询不在工作组内的其他应用
    nzOpenChange() {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.omGroups + '/' + this.groupGuid + '/availableApp')
            .subscribe(
                (val) => {
                    this.searchOptions = val.result;
                    console.log(val.result)
                });
    }


    // 查询工作组下的应用
    querygroupApp() {
        this.page = {
            page: {
                current: this.pages.pi,
                size: this.pages.size,
            }
        };

        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.omGroups + '/' + this.groupGuid + '/app', this.page)
            .subscribe(
                (val) => {
                    console.log(val.result.records)
                    for (let i = 0; i < val.result.records.length; i ++ ) {
                        if(val.result.records[i].openDate) {
                            val.result.records[i].openDate =  moment(val.result.records[i].openDate).format('YYYY-MM-DD');
                        }
                    }
                    this.data = val.result.records;
                    this.total = val.result.total;
                });


    }




    appClick() {
        console.log(this.selectedMultipleOption); // 传入后台，渲染
        const jsonObj = {
            groupCode: this.groupGuid,
            appGuidList: this.selectedMultipleOption,
        }

        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.groupApp, jsonObj)
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                    this.selectedMultipleOption = [];
                    this.querygroupApp();
                });
    }






    // 列表组件传过来的内容
    addHandler(event) {
        // 删除应用
        this.utilityService.deleatData(appConfig.serverUrl  + appConfig.API.omGroups + '/' + this.groupGuid + '/app/' + event.guid )
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                    this.querygroupApp();
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                });

    }

    deleteBatch(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条应用吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchDeleteApp, deletaObj )
                    .subscribe(
                        (val) => {
                            this.selectedRows = [];
                            this.querygroupApp();
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.selectedRows = [];
                this.querygroupApp();
            }
        });
    }


    // 列表传入的翻页数据
    monitorHandler(event) {
        console.log(event)
        this.pages.pi = event;
        this.page = {
            page: {
                current: event, // 页码
                size: this.pages.size, //  每页个数
            }
        };
        this.querygroupApp();
    }


    // 列表按钮方法
    buttonDataHandler(event) {
        console.log(event); // 根据event.value来判断不同的请求，来获取结果和方法或者进行路由的跳转
    }

    selectedRow(event) { // 选中方法，折叠层按钮显示方法
    }

    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {
        console.log(event); // 拿到数据进行判断，是跳转路由还是弹出框弹出

    }



}
